package com.yidu.DatelessManagement.service.impl;
import com.yidu.BusinessParament.entity.Fund;
import com.yidu.DatelessManagement.mapper.ProvisionOfIncomeMapper;
import com.yidu.DatelessManagement.service.ProvisionOfIncomeService;
import com.yidu.GlobalManagement.log.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.DatelessManagement.entity.ProvisionOfIncome;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周楠櫻木
 * @version 1.0
 * @date 2023/2/5
 */
@Service
public class ProvisionOfIncomeServiceImpl implements ProvisionOfIncomeService {
    @Resource
    ProvisionOfIncomeMapper provisionOfIncomeMapper;


    /**
     * 现金利息
     * @param provisionOfIncome
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<ProvisionOfIncome> selectCashInterest(ProvisionOfIncome provisionOfIncome, int page, int limit) {
        Map map = new HashMap();
        map.put("sqlWhere",getWhere(provisionOfIncome,null));
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        List<ProvisionOfIncome> provisionOfIncomeList = provisionOfIncomeMapper.selectCashInterest(map);
        for (ProvisionOfIncome ofIncome : provisionOfIncomeList) {
            //未去尾的数据
            double cashLiXi1 = ofIncome.getCashLiXi();
            //格式化数据
            double cashLixi = NumberUtil.formatNum(cashLiXi1);
            //将更改好的数据重新放入到集合中
            ofIncome.setCashLiXi(cashLixi);
        }
        return provisionOfIncomeList;
    }

    @Override
    public int getSumRow(ProvisionOfIncome provisionOfIncome) {
        Map map = new HashMap();
        String sqlWhere = getWhere(provisionOfIncome,null);
        map.put("sqlWhere",sqlWhere);
        return provisionOfIncomeMapper.getSumRow(map);
    }

    /**
     * 债券利息
     * @param provisionOfIncome
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<ProvisionOfIncome> selectBondInterest(ProvisionOfIncome provisionOfIncome, int page, int limit) {
        Map map = new HashMap();
        map.put("sqlWhere",getWhere(provisionOfIncome,null));
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        List<ProvisionOfIncome> provisionOfIncomeList = provisionOfIncomeMapper.selectBondInterest(map);
        for (ProvisionOfIncome ofIncome : provisionOfIncomeList) {
            //未去尾的数据
            double bondLiXi = ofIncome.getBondLiXi();
            //格式化数据
            double liXi = NumberUtil.formatNum(bondLiXi);
            //将更改好的数据重新放入到集合中
            ofIncome.setBondLiXi(liXi);
        }
        return provisionOfIncomeList;
    }

    /**
     *
     * @param provisionOfIncome
     * @return
     */
    @Override
    public List<ProvisionOfIncome> getSumRowBond(ProvisionOfIncome provisionOfIncome) {
        Map map = new HashMap();
        String sqlWhere = getWhere(provisionOfIncome,null);
        map.put("sqlWhere",sqlWhere);
        return provisionOfIncomeMapper.getSumRowBond(map);
    }
    /**
     * 两费查询
     * @param provisionOfIncome
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<ProvisionOfIncome> selectFund(ProvisionOfIncome provisionOfIncome, int page, int limit,String foundTime) {
        Map map = new HashMap();
        map.put("sqlWhere",getWhere(provisionOfIncome,foundTime));
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        return provisionOfIncomeMapper.selectFund(map);
    }

    @Override
    public int SelectFundCount(ProvisionOfIncome provisionOfIncome,String foundTime) {
        Map map = new HashMap();
        String sqlWhere = getWhere(provisionOfIncome,foundTime);
        map.put("sqlWhere",sqlWhere);
        return provisionOfIncomeMapper.SelectFundCount(map);
    }




    /**
     * 获取指定时间的资产净值
     *
     * @param createTime 时间
     * @return double 资产净值
     */
    @Override
    public Double getZiChanJinZhi(String createTime) {
        return provisionOfIncomeMapper.getChanJinZhi(createTime);
    }

    /**
     * 获取指定时间的资产净值行数
     *
     * @param createTime 时间
     * @return int 返回查询到的行数
     */
    @Override
    public int getChanJinZhiHangShu(String createTime) {
        return provisionOfIncomeMapper.getChanJinZhiHangShu(createTime);
    }

    /**
     * 查询基金创立时间
     *
     * @param fund 基金对象
     * @return int 查询到的行数
     */
    @Override
    public int getJiJinChuangLiShiJian(Fund fund) {
        Map<String, Object> map = new HashMap<>();
        map.put("fundId", fund.getFundId());
        map.put("foundTime", fund.getFoundTime());
        return provisionOfIncomeMapper.getJiJinChuangLiShiJian(map);
    }

    /**
     * 通过查询基金规模
     *
     * @param fundId 数据映射集合
     * @return double 基金规模
     */
    @Override
    public double getJiJinGuiMo(String fundId) {
        return provisionOfIncomeMapper.getJiJinGuiMo(fundId);
    }

    /**
     * 判断今日是否为基金成立日
     *
     * @param fund 基金对象
     * @return int 执行成功的行数
     */
    @Override
    public boolean panDuanJinRiShiFouWeiJiJingChenLiRi(Fund fund) {
        boolean judge = true;
        Map<String, Object> map = new HashMap<>();
        map.put("fundId", fund.getFundId());
        map.put("foundTime", fund.getFoundTime());
        int i = provisionOfIncomeMapper.panDuanJinRiShiFouWeiJiJingChenLiRi(map);
        if (i == 0) {
            judge = false;
        }
        return judge;
    }

    /**
     * 根据id查询基金
     *
     * @param fundId 基金id
     * @return
     */
    @Override
    public Fund provisionOfIncomeSelectFund(String fundId) {
        return provisionOfIncomeMapper.provisionOfIncomeSelectFund(fundId);
    }

    @Override
    public List<ProvisionOfIncome> SelectFundScale() {
        return provisionOfIncomeMapper.SelectFundScale();
    }


    /**
     * 模糊查询拼接条件
     * @param provisionOfIncome
     * @return
     */
    public String getWhere(ProvisionOfIncome provisionOfIncome,String foundTime) {
        StringBuilder stringBuilder = new StringBuilder();
        if (provisionOfIncome.getDate()!=null && !provisionOfIncome.getDate().equals("")) {
            stringBuilder.append(" and date LIKE '%"+provisionOfIncome.getDate()+"%'");
        }else if(provisionOfIncome.getStock_countTime()!=null && !provisionOfIncome.getStock_countTime().equals("")) {
            stringBuilder.append(" and date LIKE '%"+provisionOfIncome.getStock_countTime()+"%'");
        }
        if(foundTime!=null && !foundTime.equals("")){
            System.out.println("我进来了===================="+foundTime);
            stringBuilder.append(" and foundTime LIKE '%"+foundTime+"%'");
        }
        return stringBuilder.toString();
    }

    /*public String getWheres(ProvisionOfIncome provisionOfIncome, String foundTime) {
//        System.out.println("impl"+foundTime;
        StringBuilder stringBuilder = new StringBuilder();
        if (foundTime != null) {
            stringBuilder.append(" and foundTime LIKE '%" + foundTime + "%'");
        }
        if (provisionOfIncome.getFundId() != null && !"".equals(provisionOfIncome.getFundId())) {
//            TxtUtil.sout();
            stringBuilder.append(" and fundId ='" + provisionOfIncome.getFundId() + "'");
        }
        return stringBuilder.toString();
    }*/

}
